package dataStructure.SparseArray;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author lhn
 * @date 2024-04-09 22:07
 */

public class SparseArray {
    public static int[][] getSparseArray(int[][] chessArr) {
        int count = 0;
        for (int i = 0; i < chessArr.length; i++) {
            for (int j = 0; j < chessArr[i].length; j++) {
                if (chessArr[i][j]!=0) {
                    count++;
                }
            }
        }
        System.out.println("count is " + count);

        int sparseArr[][] = new int[count+1][3];
        sparseArr[0][0] = chessArr.length;
        sparseArr[0][1] = chessArr[0].length;
        sparseArr[0][2] = count;
        int index =  1;
        for (int i = 0; i < chessArr.length; i++) {
            for (int j = 0; j < chessArr[i].length; j++) {
                if (chessArr[i][j]!=0) {
                    sparseArr[index][0] = i;
                    sparseArr[index][1] = j;
                    sparseArr[index][2] = chessArr[i][j];
                    index ++;
                }
            }
        }
//        System.out.println("sparseArr is " + Arrays.deepToString(sparseArr));
        return sparseArr;
    }

    public static int[][] get2DArray(int[][] sparseArr){
        int array2D[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
        for (int i = 1; i < sparseArr.length; i++) {
            array2D[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
//        System.out.println("array2D is " + Arrays.deepToString(array2D));
        return array2D;
    }
    public static void saveFile(int sparseArr[][]) {
        try {
            PrintWriter writer = new PrintWriter("map.data.txt","UTF-8");
            for (int[] row : sparseArr) {
                for (int num : row) {
//                    System.out.println(num);
                    writer.print(num);
                    // 在每个元素后添加空格（如果需要）
                    writer.print(" ");
                }
                // 在每行之后添加换行符
                writer.println();
            }
            writer.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    public static int[][] readFileToArry(File file){
        List<List<Integer>> rows = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] elements = line.trim().split("\\s+");
//                System.out.println("elements is " + Arrays.deepToString(elements));
                List<Integer> row = new ArrayList<>();
                for (String element : elements) {
                    row.add(Integer.parseInt(element)); // 将字符串转换为整数
                }
                rows.add(row);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        int[][] sparseArr = new int[rows.size()][];
        for (int i = 0; i < rows.size(); i++) {
            sparseArr[i] = rows.get(i).stream().mapToInt(Integer::intValue).toArray();
        }
//        System.out.println("sparseArr is " + Arrays.deepToString(sparseArr));
        return sparseArr;
    }

    public static void traverse2DArray(int[][] array) {
        if (array == null) {
            System.out.println("二维数组为空");
            return;
        }

        for (int i = 0; i < array.length; i++) {
            if (array[i] == null) {
                System.out.println("第 " + (i + 1) + " 行数组为空");
                continue;
            }

            for (int j = 0; j < array[i].length; j++) {
                System.out.printf("%d\t",array[i][j]);
            }
            System.out.println(); // 每遍历完一行后换行
        }
    }

    public static void main(String[] args) {
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        //  原始的二维数组
        System.out.println("original 2D Array is ");
        traverse2DArray(chessArr1);
        int sparseArr[][] = getSparseArray(chessArr1);
        System.out.println("original sparse Array is ");
        traverse2DArray(sparseArr);
        //  将sparseArr保存到文件中
        saveFile(sparseArr);
        //  读取文件
        int sparseArr2[][] = readFileToArry(new File("map.data.txt"));
        System.out.println("sparse Array is ");
        traverse2DArray(sparseArr2);
        int array2D[][] = get2DArray(sparseArr2);
        System.out.println("2D Array is ");
        traverse2DArray(array2D);
    }
}
